<template>
  <div class="logincontain">
    <div class="login_form">
      <img src="../assets/logo.png" alt="" />
      <el-form class="el-login-form" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" prefix-icon="iconfont icon-3702mima" placeholder="请输入密码"></el-input>
        </el-form-item>
        <div class="el-btn-login">
          <el-button type="primary" @keyup.enter="keyDown(e)" @click="submitForm('ruleForm')">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { LoginPostData } from '../api/index.js'
export default {
  mounted() {
    window.addEventListener('keydown', this.keyDown)
  },
  data() {
    return {
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // alert('提交成功')
          const { data: res } = await LoginPostData(this.form)
          // console.log(res)
          if (res.meta.status === 200) {
            this.$message({
              showClose: true,
              message: res.meta.msg,
              type: 'success'
            })
            // console.log(res.data.token)
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.replace('/Main')
          } else {
            this.$message({
              showClose: true,
              message: res.meta.msg,
              type: 'error'
            })
          }
        } else {
          console.log('error')
          return false
        }
      })
    },
    keyDown(e) {
      if (e.keyCode === 13) {
        this.submitForm('ruleForm')
      }
    },
    resetForm() {
      // console.log(1)
      this.$refs.ruleForm.resetFields()
    }
  },
  computed: {
    ...mapState({
      form: state => state.Login.form
    })
  }
}
</script>

<style lang="less" scoped>
.logincontain {
  position: relative;
  height: 100%;
  background-color: #2b4b6b;
  .login_form {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    position: relative;
    img {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      background-color: #eee;
      // object-fit: contain;
      position: absolute;
      left: 50%;
      // top:-10 ;
      transform: translate(-50%, -30%);
    }
    .el-login-form {
      position: absolute;
      bottom: 0px;
      padding: 20px 20px;
      width: 100%;
      box-sizing: border-box;
      .el-btn-login {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
